package furns.web;

import furns.entity.Furn;
import furns.entity.Page;
import furns.service.FurnService;
import furns.service.impl.FurnServiceImpl;
import furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class CustomerFurnServlet extends BasicServlet{
    private FurnService furnService = new FurnServiceImpl();
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取数据，转换成int型
        int pageNo = DataUtils.strPareInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.strPareInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //封装成page
        Page<Furn> page = furnService.page(pageNo, pageSize);
        req.setAttribute("page",page);
        System.out.println(req.getContextPath());
        //转发
        req.getRequestDispatcher("/customer/index.jsp").forward(req,resp);
    }


    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //1.如果参数有name 带上没有值, 接收到的是""
        //2.如果name参数都没有, 接收到的null
        //3.也就是说我们把 "" 和 null 业务逻辑合并在一起..
        String name = req.getParameter("name");
        System.out.println(name);
        if (name == null){
            name = "";
        }
        int pageNo = DataUtils.strPareInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.strPareInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //调用service方法, 获取Page对象
        Page<Furn> pageByName = furnService.getPageByName(pageNo, pageSize, name);
        StringBuilder url = new StringBuilder("customerFurnServlet?action=pageByName");
        if(!"".equals(name)){//如name不为"", 则在拼接 name参数
            url.append("&name=").append(name);
        }
        pageByName.setUrl(url.toString());
        req.setAttribute("pageByName",pageByName);
        //请求转发到furn_manage.jsp
        req.getRequestDispatcher("/customer/index.jsp").forward(req,resp);
    }
}
